#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

class EllysSki
{
public:
    int getMax(vector <int> height)
    {
        int ans;
        vector<int> ansvec;
        for(int i=0;i<height.size();i++)
        {
            ans=1;
            for(int j=i+1;j<height.size();j++)
            {
                if(height[j]>height[j-1])
                    break;
                ++ans;
            }
            ansvec.push_back(ans);
            ans=1;
            for(int j=i-1;j>=0;j--)
            {
                if(height[j]>height[j+1])
                    break;
                ++ans;
            }
            ansvec.push_back(ans);
        }
        for(vector<int>::iterator it=ansvec.begin();it!=ansvec.end();it++)
            ans=max(ans,*it);
        return ans;
    }

};
class EllysTeleport
{
public:
   int getMax(int N, int H1, int A, int B, int P, int Q, int M)
   {
       int ans=0,maxAns=0;
       vector<long long > height;
       vector<bool> coin(N,0);
       height.push_back(H1);
       for(int i=1;i<N;i++)
           height.push_back((height[i-1]*A+B)%M);
       sort(height.begin(),height.end());
       for(int i=0;i<N;i++)
       {
           ans=0;
           for(int j=0;j<N;j++)
               coin[j]=0;        
           coin[i]=1;
           teleport(height,coin,P,Q,M,i);
           for(int j=0;j<N;j++)
               if(coin[j]==1)++ans;
           maxAns=max(maxAns,ans);
       }
       return maxAns;

   }
   void teleport(vector<long long> height,vector<bool> & coin,int P, int Q, int M,int a)
   {
       int Y,bb=a;
       Y=(height[a]*P+Q)%M;
       if(height[0]>Y)
           return;
       for(int i=0;i<height.size();i++)
       {
           if(Y>height.back())
           {
               if(coin[height.size()-1]==0)
                   coin[height.size()-1]=1;
               else
                   return;
            bb=height.size()-1;
            break;
           }

           if(height[i]==Y)
           {
               if(coin[i]==0)
                   coin[i]=1;
               else
                   return;
            bb=i;
            break;
           }
           if(height[i]>Y)
           {
               if(coin[i-1]==0)
                   coin[i-1]=1;
               else
                   return;
            bb=i-1;
            break;
           }
       }
       teleport(height,coin,P,Q,M,bb);
       return;
   }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    EllysSki test;
    vector<int> height{50, 77, 24, 86, 98, 84, 42, 70, 88, 78, 73, 17, 76, 68, 64, 65, 40, 77, 33, 87, 11, 23, 78, 20, 8, 74, 44, 95, 94, 78, 27, 88, 71, 40, 11, 98, 82, 85, 79, 89, 31, 67, 41, 61, 71, 62, 74, 77, 86, 36};
//    cout<<test.getMax(height);
    EllysTeleport test1;
    cout<<"answer is "<<test1.getMax(1000,1337,706135,1085680,1153206,345473,1234567);
    return a.exec();
}
